#!/usr/bin/env python
#coding=utf-8

class PreferencesPresenter(object):
    ''' Take care of the applications preferences. '''

    def __init__(self, model, view, interactor):
        ''' Needs data object, gui elements and event handler. '''

        self.model = model
        self.view = view
        interactor.Install(self, view)
        self.load_model()
        view.start()

    def load_model(self):
        ''' Fill the input fields with data from the model. '''

        self.view.set_dbfile(self.model.config.get('Settings','dbfile'))
        self.view.set_error_offset(
                        self.model.config.get('Settings','errorOffset'))
        self.view.set_lang1(self.model.config.get('Settings','lang1'))
        self.view.set_lang2(self.model.config.get('Settings','lang2'))
        self.view.set_voicePrg(self.model.config.get('Settings','voiceProgram'))
        self.view.set_category_default(self.model.config.get('Settings', 'defaultCategory'))
        if self.model.config.get('Settings','voiceProgram') == 'festival':
            self.view.set_voice_prg_location(
                self.model.config.get('Settings','festivalPath'))
        elif self.model.config.get('Settings','voiceProgram') == 'espeak':
            self.view.set_voice_prg_location(
                self.model.config.get('Settings','espeakPath'))

    def set_voice_prg_path(self):
        '''Set path according to voiceOutputCtrl.'''
        prg = self.view.get_voiceOutputPrg()
        if prg == 'festival':
            self.view.set_voice_prg_location(
                self.model.config.get('Settings','festivalPath'))
        elif prg == 'espeak':
            self.view.set_voice_prg_location(
                self.model.config.get('Settings','espeakPath'))


    def quit(self):
        ''' Close the Preferences Window without saving changes. '''

        self.view.quit()

    def save(self):
        ''' Save the data from the gui. '''

        readyToSave = True
        errorOffset = self.view.get_offset()
        dbfile = self.view.get_dbfile()
        lang1 = self.view.get_lang1()
        lang2 = self.view.get_lang2()
        voiceOutputPrg = self.view.get_voiceOutputPrg()
        voicePrgLocation = self.view.get_voice_prg_location()
        defaultCategory = self.view.get_default_category()

        try:
            errorOffset = str(int(errorOffset))
        except ValueError:
            readyToSave = False
            self.view.show_message(
                'Please enter a numerical value for the Error offset')

        if dbfile == '':
            readyToSave = False
            self.view.show_message('Please enter a valid filename')

        if dbfile <> self.model.config.get('Settings','dbfile'):
            self.view.show_message(\
                'Changes will take effect after restarting pyVoc')
        if lang1 == '' or lang2=='':
            readyToSave = False
            self.view.show_message('Please enter a language')

        if readyToSave:
            self.model.update_config(dbfile, errorOffset, lang1, lang2,
                voiceOutputPrg, voicePrgLocation, defaultCategory)
            self.view.show_message('Preferences saved.\n'\
                'The changes will take effect after restarting pyVoc')
            self.view.quit()
